package com.lh.item.controller;

import com.lh.item.pojo.SpecGroup;
import com.lh.item.pojo.SpecParam;
import com.lh.item.service.ISpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品规格管理
 *
 * @author liuhe
 * @date 2020/06/23
 */
@RestController
@RequestMapping("spec")
public class SpecificationController {

    final ISpecificationService iSpecificationService;

    @Autowired
    public SpecificationController(ISpecificationService iSpecificationService) {
        this.iSpecificationService = iSpecificationService;
    }

    /**
     * 查询所有分组规格
     */
    @GetMapping("groups/{cid}")
    public ResponseEntity<List<SpecGroup>> querySpecGroups(@PathVariable("cid") Long cid) {
        List<SpecGroup> specGroups = this.iSpecificationService.querySpecGroup(cid);
        if (CollectionUtils.isEmpty(specGroups)) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(specGroups);
    }

    /**
     * 根据gid/cid查询规格参数
     */
    @GetMapping("params")
    public ResponseEntity<List<SpecParam>> querySpecParam(@RequestParam(value = "gid", required = false) Long gid,
                                                             @RequestParam(value = "cid", required = false) Long cid,
                                                             @RequestParam(value = "generic", required = false) Boolean generic,
                                                             @RequestParam(value = "searching", required = false) Boolean searching) {
        List<SpecParam> specParams = this.iSpecificationService.queryParams(gid, cid, generic, searching);
        if (CollectionUtils.isEmpty(specParams)) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(specParams);
    }

    /**
     * 查询规格组及组内参数
     */
    @GetMapping("{cid}")
    public ResponseEntity<List<SpecGroup>> querySpecsByCid(@PathVariable("cid") Long cid) {
        List<SpecGroup> specGroups = this.iSpecificationService.querySpecsByCid(cid);
        if (CollectionUtils.isEmpty(specGroups)) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(specGroups);
    }
}
